package org.example.mq.demo;

import org.example.mq.common.Consumer;
import org.example.mq.common.MqException;
import org.example.mq.common.network.mqclient.Channel;
import org.example.mq.common.network.mqclient.Connection;
import org.example.mq.common.network.mqclient.ConnectionFactory;
import org.example.mq.mqserver.core.BasicProperties;
import org.example.mq.mqserver.coreenum.ExchangeType;

import java.io.IOException;

public class DemoConsumer {
    public static void main(String[] args) throws IOException, MqException, InterruptedException {
        System.out.println("启动消费者");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(9090);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //创建交换机和队列
        channel.exchangeDeclare("testExchange", ExchangeType.DIRECT,true,true,null);
        channel.queueDeclare("testQueue",true,true,true,null);

        channel.basicConsume("testQueue", true, new Consumer() {
            @Override
            public void handleDelivery(String consumerTag, BasicProperties basicProperties, byte[] body) throws MqException, IOException {
                System.out.println("开始消费数据");
                System.out.println("结束消费数据");
            }
        });

        while (true){
            Thread.sleep(500);
        }
    }
}
